package com.googlecode.droidircclient;
/* =================================================
 * Droid IRC Client
 * Authors: Kevin Garton, Dennis McWherter
 * 4 October 2011
 * =================================================*/

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;

public class ServerInfoActivity extends PreferenceActivity {
	public static final String PREFS_FILE = ".droidIRC";
	private EditTextPreference hostpref;
	private EditTextPreference portpref;
	private SharedPreferences prefs;
	
	/**
	 * onCreate
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.server_info);
		
		prefs    = getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
		hostpref = (EditTextPreference)findPreference("ServerPref");
		portpref = (EditTextPreference)findPreference("PortPref");
		
		loadPreferences();
	}
	
	/**
	 * onStop
	 */
	@Override
	protected void onStop() {
		super.onStop();
		savePreferences();
	}
	
	/**
	 * loadPreferences
	 */
	private void loadPreferences()
	{
		String server = prefs.getString("server", "irc.freenode.net");
		int port      = prefs.getInt("port", 6667);

		// Update the visible fields
		hostpref.setText(server);
		portpref.setText(Integer.toString(port));
	}
	
	/**
	 * savePreferences
	 */
	private void savePreferences() {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("server", hostpref.getText());
		editor.putInt("port", Integer.parseInt(portpref.getText().toString()));
		editor.commit();
	}
}
